package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {


    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    /**
     * 更新vip失效状态。
     * @return
     */
    @XxlJob("updateVipExpireStatusJob")
    public Result updateVipExpireStatusJob(){
        //  开始时间
        long startTime = System.currentTimeMillis();
        //  创建日志实体类
        XxlJobLog xxlJobLog = new XxlJobLog();
        try {
            //  设置jobId
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            //  远程调用方法。
            Result result = userInfoFeignClient.updateVipExpireStatus();
            //  判断：
            if (200!=result.getCode()){
                //  执行失败.
                throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
            }
            //  执行成功
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            //  执行失败.
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
        } finally {
            Integer times = Math.toIntExact(System.currentTimeMillis() - startTime);
            xxlJobLog.setTimes(times);
            xxlJobLogMapper.insert(xxlJobLog);
        }

        //  返回数据
        return Result.ok();
    }

    //  更新排行榜：
    @XxlJob("updateLatelyAlbumRankingJob")
    public Result updateLatelyAlbumRanking(){
        //  开始时间
        long startTime = System.currentTimeMillis();
        //  创建日志实体类
        XxlJobLog xxlJobLog = new XxlJobLog();
        try {
            //  设置jobId
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            //  调用远程方法.
            Result result = searchFeignClient.updateLatelyAlbumRanking();
            //  判断
            if (200!=result.getCode()){
                //  执行失败.
                throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
            }
            //  执行成功
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            //  执行失败.
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
        } finally {
            Integer times = Math.toIntExact(System.currentTimeMillis() - startTime);
            xxlJobLog.setTimes(times);
            xxlJobLogMapper.insert(xxlJobLog);
        }
        //  默认返回数据
        return Result.ok();
    }

}